package com.hankol.newretail.viewmodel

import android.app.Activity
import androidx.lifecycle.MutableLiveData
import com.baselibrary.adapter.ninegrid.ImageInfo
import com.baselibrary.base.BasicListBean
import com.baselibrary.base.mvvm.BaseViewModel
import com.baselibrary.bean.MediaInfo
import com.hankol.newretail.bean.*
import com.hankol.newretail.respository.NewReatilRequestManager
import com.hankol.newretail.utils.RetailPopUtils

/**
 * @author: 关忆北、
 * @date: 2022/8/10
 * @Des：
 */
class AfterSalesViewModel : BaseViewModel() {

    val reasonStrList: ArrayList<String> = arrayListOf("大小/重量与商品描述不符", "生产日期/保质期与商品描述不符", "标签/批次/包装/成分等与商品描述不符", "商品变质/发霉/有异物", "质量问题", "其他")

    //订单详情
    var orderLoadDetailLD: MutableLiveData<OrderLoadDetailBean> = MutableLiveData()
    var applyAfterSaleLD: MutableLiveData<AfterSalesInfoBean> = MutableLiveData()
    var afterSaleListLD: MutableLiveData<BasicListBean<AfterSaleListBean>> = MutableLiveData()

    //售后详情
    var afterSaleInfoLD: MutableLiveData<AfterSalesInfoBean> = MutableLiveData()
    var revokeAfterSaleLD: MutableLiveData<AfterSalesRevokeBean> = MutableLiveData()

    fun reqAfterSaleList(page: Int) {
        NewReatilRequestManager.getInstance().reqAfterSaleList(page, 20, afterSaleListLD)
    }

    fun reqRevokeAfterSale(afterSaleId: Long) {
        NewReatilRequestManager.getInstance().reqRevokeAfterSale(afterSaleId, revokeAfterSaleLD)
    }

    //订单详情 poId：商品订单id
    fun requestOrderDetail(poId: String) {
        NewReatilRequestManager.getInstance().requestOrderDetail(orderLoadDetailLD, poId)

    }

    //提交售后
    fun reqApplyAfterSale(json: String) {
        NewReatilRequestManager.getInstance().reqApplyAfterSale(json, applyAfterSaleLD)
    }

    //售后详情
    fun reqLoadAfterSaleInfo(afterSaleId: Long) {
        NewReatilRequestManager.getInstance().reqLoadAfterSale(afterSaleId, afterSaleInfoLD)
    }

    /**
     * 显示退款原因
     */
    fun showRefundReasonView(activity: Activity, onRefundReason: OnRefundReasonCallBack) {
        val reasonList = mutableListOf<CancelReasonListBean>()
        reasonStrList.forEachIndexed { index, s ->
            reasonList.add(CancelReasonListBean(index.plus(1), s, false))
        }
        RetailPopUtils.showRefundedReasonDialog(activity, reasonList, object : RetailPopUtils.ICallBack4BackMoneyReson {
            override fun onClickOk(bean: CancelReasonListBean) {
                onRefundReason.onRefundReason(bean.desc, bean.cancellationId)
            }
        })
//        val pickerPopupWindow = PickerPopupWindow(activity)
//        pickerPopupWindow.showAtLocation(activity.window.decorView, Gravity.BOTTOM, 0, 0)
//        pickerPopupWindow.setTitle("退款原因")
//        pickerPopupWindow.setAgeList(reasonStrList)
//        pickerPopupWindow.setOnClickListener(object : OnClickListener {
//            override fun onClick(view: View, str: Any) {
//                if (str is String) {
//                    for (i in reasonStrList.indices) {
//                        if (str == reasonStrList[i]) {
//                            //选择的信息
//                            onRefundReason.onRefundReason(str, i + 1)
//                        }
//                    }
//                }
//            }
//
//            override fun onLongClick(view: View, `object`: Any) {
//
//            }
//        })
    }

    fun getVoucher(list: List<MediaInfo>): MutableList<ImageInfo> {
        val imageInfo = mutableListOf<ImageInfo>()
        list.forEach {
            val imInfo = ImageInfo()
            imInfo.thumbnailUrl = it.coverUrl
            imInfo.bigImageUrl = it.coverUrl
            imageInfo.add(imInfo)
        }
        return imageInfo
    }

    interface OnRefundReasonCallBack {
        /**
         * @param reasonPos 下表索引，0、1、2、3、4  需要+1
         */
        fun onRefundReason(str: String, reasonPos: Int)
    }

}